import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isNumber from "lodash/isNumber";

/**
 * Col
 * @date 2018/10/10
 */
import React from 'react';
import { Input, InputNumber, DatePicker, Col as CpaasCol } from 'antd';
import Lov from 'components/Lov';
import ValueList from 'components/ValueList';
import Switch from 'components/Switch';
import Checkbox from 'components/Checkbox';
import { emptyFieldType, fieldLabelProp } from "../../../config";
import CellControl from "./CellControl";
import styles from "../../index.less?modules";
var componentType = {
  Lov: Lov,
  ValueList: ValueList,
  Switch: Switch,
  Checkbox: Checkbox,
  Input: Input,
  InputNumber: InputNumber,
  TextArea: Input.TextArea,
  DatePicker: DatePicker
};

function getFieldProps(component) {
  var fieldProps = {};

  if (component.enabledFlag !== 1) {
    fieldProps.disabled = true;
  }

  switch (component.componentType) {
    case 'DatePicker':
      fieldProps.placeholder = '';
      break;

    default:
      break;
  }

  return fieldProps;
}

var Col = /*#__PURE__*/function (_React$Component) {
  _inherits(Col, _React$Component);

  var _super = _createSuper(Col);

  function Col() {
    _classCallCheck(this, Col);

    return _super.apply(this, arguments);
  }

  _createClass(Col, [{
    key: "render",
    value: function render() {
      var _this$props$fieldLabe = this.props.fieldLabelWidth,
          fieldLabelWidth = _this$props$fieldLabe === void 0 ? 120 : _this$props$fieldLabe;
      var _this$props = this.props,
          component = _this$props.component,
          wrapperFieldComponent = _this$props.wrapperFieldComponent,
          pComponent = _this$props.pComponent,
          onRemoveField = _this$props.onRemoveField,
          rowIndex = _this$props.rowIndex,
          colIndex = _this$props.colIndex,
          rowCount = _this$props.rowCount,
          colCount = _this$props.colCount,
          currentEditField = _this$props.currentEditField,
          onUpdateComponent = _this$props.onUpdateComponent,
          willRemovePositionY = _this$props.willRemovePositionY,
          willRemovePositionX = _this$props.willRemovePositionX,
          positionY = _this$props.positionY,
          positionX = _this$props.positionX,
          positionRightX = _this$props.positionRightX,
          onUpdateWillRemovePositionY = _this$props.onUpdateWillRemovePositionY,
          onUpdateWillRemovePositionX = _this$props.onUpdateWillRemovePositionX,
          onRefreshTemplate = _this$props.onRefreshTemplate;
      var FieldType = componentType[component.componentType] || Input;
      var _pComponent$fields = pComponent.fields,
          fields = _pComponent$fields === void 0 ? [] : _pComponent$fields;
      var menuItemProps = {
        canInsertRight: true,
        canInsertBottom: true
      }; // 可以删除列,行

      if (colCount > 1) {
        menuItemProps.canRemoveCol = true;
      }

      if (rowCount > 1) {
        menuItemProps.canRemoveRow = true;
      } // 合并


      if (fields[rowIndex] && fields[rowIndex][colIndex + 1] && fields[rowIndex][colIndex + 1].componentType === emptyFieldType) {
        menuItemProps.canMergeRight = true;
      } // 取消合并


      if (component.colspan && _isNumber(component.colspan)) {
        menuItemProps.canCancelMergeRight = true;
      }

      var fieldOptions = {
        rowIndex: rowIndex,
        colIndex: colIndex
      }; // 字段是不是必输

      var isRequired = component.requiredFlag === 1;
      return /*#__PURE__*/React.createElement(CpaasCol, getColLayout(colCount, component.colspan), wrapperFieldComponent( /*#__PURE__*/React.createElement(CellControl, {
        component: component,
        pComponent: pComponent,
        menuItemProps: menuItemProps,
        onRemoveField: onRemoveField,
        currentEditField: currentEditField,
        fieldOptions: fieldOptions,
        colCount: colCount,
        rowIndex: rowIndex,
        colIndex: colIndex,
        onUpdateComponent: onUpdateComponent,
        willRemovePositionY: willRemovePositionY,
        willRemovePositionX: willRemovePositionX,
        positionY: positionY,
        positionX: positionX,
        positionRightX: positionRightX,
        onUpdateWillRemovePositionX: onUpdateWillRemovePositionX,
        onUpdateWillRemovePositionY: onUpdateWillRemovePositionY,
        onRefresh: onRefreshTemplate
      }, component.componentType === emptyFieldType ? /*#__PURE__*/React.createElement("div", {
        className: "".concat(styles['dynamic-form-item'], " ").concat(styles['dynamic-form-item-placeholder'])
      }) : /*#__PURE__*/React.createElement("div", {
        className: styles['dynamic-form-item']
      }, /*#__PURE__*/React.createElement("div", {
        className: "".concat(styles['dynamic-form-item-label'], " ").concat(isRequired ? styles.required : ''),
        style: {
          width: fieldLabelWidth,
          minWidth: fieldLabelWidth,
          maxWidth: fieldLabelWidth
        }
      }, component[fieldLabelProp], ":\xA0"), /*#__PURE__*/React.createElement("div", {
        className: styles['dynamic-form-item-control']
      }, React.createElement(FieldType, getFieldProps(component))))), component, fieldOptions));
    }
  }]);

  return Col;
}(React.Component);

export { Col as default };

function getColLayout(colCount) {
  var colspan = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  return {
    span: Math.floor(24 * (colspan || 1) / colCount)
  };
}

if (process.env.NODE_ENV !== 'production') {
  Col.displayName = 'DynamicForm(Col)';
}